<template>
  <div>
    <!--  1. show-checkbox 显示选择框
          2. default-expand-all 默认展开
          3. check-strictly  设置true，可以关闭父子关联 -->
    <el-tree
      ref="refTree"
      :data="list"
      :props="{label: 'name'}"
      accordion
      default-expand-all
      show-checkbox
      check-strictly
      node-key="id"
    />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="$emit('close')">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { arrayToTree } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    curId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    // 获取所有权限点
    this.loadPermission()
    // 根据id获取，当前角色拥有的权限点
    // this.loadRoleDetail()
  },
  methods: {
    async hSubmit() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      console.log(permIds)
      await assignPerm({ id: this.curId, permIds: permIds })
      // 提示
      this.$message.success('保存成功')
      // 通知父组件 关闭弹窗
      this.$emit('close')
    },
    // 加载权限
    async loadPermission() {
      const { data: res } = await getPermissionList()
      // console.log(res)
      // 数据转树形结构
      this.list = arrayToTree(res)
    },
    // 加载角色详细信息
    async loadRoleDetail() {
      const { data: res } = await getRoleDetail(this.curId)
      console.log('Role', res)
      // 数据回填
      this.$refs.refTree.setCheckedKeys(res.permIds)
    }
  }
}
</script>

<style>

</style>
